load spectrum;
%3.1
%figure(1);
FS=20000;
f1=2500;
L=256;
Ts=1/FS;
OM=2*pi*f1*Ts;
n=1:L;
xn=sin(n.*OM);
figure(1);
stem(n,xn);

%3.2
figure(2);
load spectrum;
semilogy(f,fresponse);

%3.3
figure(3);
fftflops;

%3.4
figure(4);
Y = fft(xn,256)/L;
f = FS*linspace(0,1,L);
% Plot single-sided amplitude spectrum.
semilogy(f,2*abs(Y));
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

%3.6
N=200;
n2=1:N;
xn2=sin(n2.*OM);
X1=fft(xn2,N)/N;
X2=fft(xn2,L)/L;
fn = FS*linspace(0,1,N);
fl = FS*linspace(0,1,L);
figure(5);
subplot(1,2,1);
semilogy(fn,abs(X1));
subplot(1,2,2);
semilogy(fl,abs(X2));

%3.7
figure(6);
ws=21; %windowsize
wr=1:ws; %windowrange
r = FS/2*linspace(0,1,256);
rw = rectwin(ws);
tr = triang(ws);
ha = hamming(ws);
ka = kaiser(ws,5.65);
subplot(1,2,1);
plot(wr,rw,wr,tr,wr,ha,wr,ka);
legend('rectwin','triangle','hamming','kaiser');
subplot(1,2,2);
frw=abs(fft(rw,512));
ftr=abs(fft(tr,512));
fha=abs(fft(ha,512));
fka=abs(fft(ka,512));
semilogy(r,frw(1:256),r,ftr(1:256),r,fha(1:256),r,fka(1:256));
legend('rectwin','triangle','hamming','kaiser');

figure(7);
ka3 = kaiser(ws,3);
ka5 = kaiser(ws,5);
ka7 = kaiser(ws,7);
ka9 = kaiser(ws,9);
subplot(1,2,1);
plot(wr,ka3,wr,ka5,wr,ka7,wr,ka9);
legend('\beta=3','\beta=5','\beta=7','\beta=9');
fka3=abs(fft(ka3,512));
fka5=abs(fft(ka5,512));
fka7=abs(fft(ka7,512));
fka9=abs(fft(ka9,512));
subplot(1,2,2);
semilogy(r,fka3(1:256),r,fka5(1:256),r,fka7(1:256),r,fka9(1:256));
legend('\beta=3','\beta=5','\beta=7','\beta=9');

%3.8
load problem3_8;
fry = abs(fft(rectwin(L).*y));
fty = abs(fft(triang(L).*y));
fhy = abs(fft(hamming(L).*y));
f3y = abs(fft(kaiser(L,3).*y));
f5y = abs(fft(kaiser(L,5).*y));
f7y = abs(fft(kaiser(L,7).*y));
f9y = abs(fft(kaiser(L,9).*y));
l=Fs*linspace(0,1,L);
figure(8);
subplot(1,2,1);
semilogy(l,fry,l,fty,l,fhy,l,f9y);
legend('rectwin','triangle','hamming','kaiser \beta=9');

subplot(1,2,2);
semilogy(l,f3y,l,f5y,l,f7y,l,f9y);
legend('\beta=3','\beta=5','\beta=7','\beta=9');

%figure;semilogy(l,f9y./(max(f9y)));